{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   instant      dteday  season  yr  mnth  holiday  weekday  workingday  \\\n0        1  2011-01-01       1   0     1        0        6           0   \n1        2  2011-01-02       1   0     1        0        0           0   \n2        3  2011-01-03       1   0     1        0        1           1   \n3        4  2011-01-04       1   0     1        0        2           1   \n4        5  2011-01-05       1   0     1        0        3           1   \n\n   weathersit      temp     atemp       hum  windspeed  casual  registered  \\\n0           2  0.344167  0.363625  0.805833   0.160446     331         654   \n1           2  0.363478  0.353739  0.696087   0.248539     131         670   \n2           1  0.196364  0.189405  0.437273   0.248309     120        1229   \n3           1  0.200000  0.212122  0.590435   0.160296     108        1454   \n4           1  0.226957  0.229270  0.436957   0.186900      82        1518   \n\n    cnt  \n0   985  \n1   801  \n2  1349  \n3  1562  \n4  1600  \n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import pandas as pd\n",
    "\n",
    "# 一些常规设置\n",
    "plt.rcParams.update({\n",
    "    \"legend.fontsize\": \"x-large\",\n",
    "    \"figure.figsize\": (30, 10),\n",
    "    \"xtick.labelsize\": \"x-large\",\n",
    "    \"ytick.labelsize\": \"x-large\",\n",
    "    \"axes.labelsize\": \"x-large\",\n",
    "    \"axes.titlesize\": \"x-large\",\n",
    "})\n",
    "sns.set_style(\"whitegrid\")\n",
    "sns.set_context(\"talk\")\n",
    "pd.set_option(\"max_colwidth\", 600)\n",
    "\n",
    "# 读取数据\n",
    "train = pd.read_csv(\"data/bike_sharing/day.csv\")\n",
    "\n",
    "# 数据探索--基础操作\n",
    "print(train.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\nRangeIndex: 731 entries, 0 to 730\nData columns (total 16 columns):\ninstant       731 non-null int64\ndteday        731 non-null object\nseason        731 non-null int64\nyr            731 non-null int64\nmnth          731 non-null int64\nholiday       731 non-null int64\nweekday       731 non-null int64\nworkingday    731 non-null int64\nweathersit    731 non-null int64\ntemp          731 non-null float64\natemp         731 non-null float64\nhum           731 non-null float64\nwindspeed     731 non-null float64\ncasual        731 non-null int64\nregistered    731 non-null int64\ncnt           731 non-null int64\ndtypes: float64(4), int64(11), object(1)\nmemory usage: 88.6+ KB\nNone\n"
     ]
    }
   ],
   "source": [
    "# 获得字段类型和记录数量\n",
    "print(train.info())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          instant      season          yr        mnth     holiday     weekday  \\\ncount  731.000000  731.000000  731.000000  731.000000  731.000000  731.000000   \nmean   366.000000    2.496580    0.500684    6.519836    0.028728    2.997264   \nstd    211.165812    1.110807    0.500342    3.451913    0.167155    2.004787   \nmin      1.000000    1.000000    0.000000    1.000000    0.000000    0.000000   \n25%    183.500000    2.000000    0.000000    4.000000    0.000000    1.000000   \n50%    366.000000    3.000000    1.000000    7.000000    0.000000    3.000000   \n75%    548.500000    3.000000    1.000000   10.000000    0.000000    5.000000   \nmax    731.000000    4.000000    1.000000   12.000000    1.000000    6.000000   \n\n       workingday  weathersit        temp       atemp         hum   windspeed  \\\ncount  731.000000  731.000000  731.000000  731.000000  731.000000  731.000000   \nmean     0.683995    1.395349    0.495385    0.474354    0.627894    0.190486   \nstd      0.465233    0.544894    0.183051    0.162961    0.142429    0.077498   \nmin      0.000000    1.000000    0.059130    0.079070    0.000000    0.022392   \n25%      0.000000    1.000000    0.337083    0.337842    0.520000    0.134950   \n50%      1.000000    1.000000    0.498333    0.486733    0.626667    0.180975   \n75%      1.000000    2.000000    0.655417    0.608602    0.730209    0.233214   \nmax      1.000000    3.000000    0.861667    0.840896    0.972500    0.507463   \n\n            casual   registered          cnt  \ncount   731.000000   731.000000   731.000000  \nmean    848.176471  3656.172367  4504.348837  \nstd     686.622488  1560.256377  1937.211452  \nmin       2.000000    20.000000    22.000000  \n25%     315.500000  2497.000000  3152.000000  \n50%     713.000000  3662.000000  4548.000000  \n75%    1096.000000  4776.500000  5956.000000  \nmax    3410.000000  6946.000000  8714.000000  \n"
     ]
    }
   ],
   "source": [
    "# 查看数据统计信息\n",
    "print(train.describe())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3    188\n2    184\n1    181\n4    178\nName: season, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# 研究每个离散型数据的统计分布\n",
    "\n",
    "# 研究season的不同取值的出现次数\n",
    "print(train[\"season\"].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1    366\n0    365\nName: yr, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# 研究yr的不同取值的出现次数\n",
    "print(train[\"yr\"].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12    62\n10    62\n8     62\n7     62\n5     62\n3     62\n1     62\n11    60\n9     60\n6     60\n4     60\n2     57\nName: mnth, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# 研究mnth的不同取值的出现次数\n",
    "print(train[\"mnth\"].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    710\n1     21\nName: holiday, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# 研究holiday的不同取值的出现次数\n",
    "print(train[\"holiday\"].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6    105\n1    105\n0    105\n5    104\n4    104\n3    104\n2    104\nName: weekday, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# 研究weeekday的不同取值的出现次数\n",
    "print(train[\"weekday\"].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1    500\n0    231\nName: workingday, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# 研究workingday的不同取值的出现次数\n",
    "print(train[\"workingday\"].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1    463\n2    247\n3     21\nName: weathersit, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# 研究weathersit的不同取值的出现次数\n",
    "print(train[\"weathersit\"].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究season和cnt的关系(scatterplot)\n",
    "sns.scatterplot(x=\"season\", y=\"cnt\", data=train[[\"season\", \"cnt\"]])\n",
    "plt.xlabel(\"season\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.title(\"cnt and season relationship\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![season和cnt的关系-scatterplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/scatter_season_cnt.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究temp和cnt的关系(scatterplot)\n",
    "sns.scatterplot(x=\"temp\", y=\"cnt\", data=train[[\"temp\", \"cnt\"]])\n",
    "plt.title(\"cnt and temp relationship\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![temp和cnt的关系-scatterplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/scatter_temp_cnt.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究temp和cnt的关系(lineplot)\n",
    "sns.lineplot(x=\"temp\", y=\"cnt\", data=train[[\"temp\", \"cnt\"]])\n",
    "plt.title(\"cnt and temp relationship\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![windspeed和cnt的关系-scatterplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/scatter_windspeed_cnt.png)\n",
    "![windspeed和cnt的关系-lineplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/line_windspeed_cnt.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究temp和cnt的关系(scatterplot)\n",
    "sns.scatterplot(x=\"temp\", y=\"cnt\", data=train[[\"temp\", \"cnt\"]])\n",
    "sns.set(style=\"darkgrid\")\n",
    "plt.title(\"cnt and temp relationship\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![temp和cnt的关系-scatterplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/scatter_temp_cnt_darkgrid.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究atemp和cnt的关系(scatterplot和lineplot)\n",
    "sns.scatterplot(x=\"atemp\", y=\"cnt\", data=train[[\"atemp\", \"cnt\"]])\n",
    "plt.title(\"cnt and atemp relationship\")\n",
    "plt.xlabel(\"atemp\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()\n",
    "\n",
    "sns.lineplot(x=\"atemp\", y=\"cnt\", data=train[[\"atemp\", \"cnt\"]])\n",
    "plt.title(\"cnt and atemp relationship\")\n",
    "plt.xlabel(\"atemp\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![atemp和cnt的关系-scatterplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/scatter_atemp_cnt.png)\n",
    "![atemp和cnt的关系-lineplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/line_atemp_cnt.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究hum和cnt的关系(scatterplot和lineplot)\n",
    "sns.scatterplot(x=\"hum\", y=\"cnt\", data=train[[\"hum\", \"cnt\"]])\n",
    "plt.title(\"cnt and hum relationship\")\n",
    "plt.xlabel(\"hum\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()\n",
    "\n",
    "sns.lineplot(x=\"hum\", y=\"cnt\", data=train[[\"hum\", \"cnt\"]])\n",
    "plt.title(\"cnt and hum relationship\")\n",
    "plt.xlabel(\"hum\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![hum和cnt的关系-scatterplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/scatter_hum_cnt.png)\n",
    "![hum和cnt的关系-lineplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/line_hum_cnt.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究windspeed和cnt的关系(scatterplot和lineplot)\n",
    "sns.scatterplot(x=\"windspeed\", y=\"cnt\", data=train[[\"windspeed\", \"cnt\"]])\n",
    "plt.title(\"cnt and windspeed relationship\")\n",
    "plt.xlabel(\"windspeed\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()\n",
    "\n",
    "sns.lineplot(x=\"windspeed\", y=\"cnt\", data=train[[\"windspeed\", \"cnt\"]])\n",
    "plt.title(\"cnt and windspeed relationship\")\n",
    "plt.xlabel(\"windspeed\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![windspeed和cnt的关系-scatterplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/scatter_windspeed_cnt.png)\n",
    "![windspeed和cnt的关系-lineplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/line_windspeed_cnt.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究temp和cnt的关系，再按照season对点分类(scatterplot带hue)\n",
    "sns.scatterplot(x=\"temp\", y=\"cnt\", data=train[[\"temp\", \"cnt\", \"season\"]], hue=\"season\")\n",
    "plt.title(\"cnt and temp relationship(divided by season)\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![temp和cnt的关系-scatterplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/scatter_temp_cnt_season.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究temp和cnt的关系，再按照yr对点分类(scatterplot带hue)\n",
    "sns.scatterplot(x=\"temp\", y=\"cnt\", data=train[[\"temp\", \"cnt\", \"yr\"]], hue=\"yr\")\n",
    "plt.title(\"cnt and temp relationship(divided by yr)\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![temp和cnt的关系-scatterplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/scatter_temp_cnt_yr.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究temp和cnt的关系, 再按照holiday对点分类(scatterplot带hue和style)\n",
    "sns.scatterplot(x=\"temp\", y=\"cnt\", data=train[[\"temp\", \"cnt\", \"holiday\"]], hue=\"holiday\", style=\"holiday\")\n",
    "plt.title(\"cnt and temp relationship(divided by holiday)\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![temp和cnt的关系-scatterplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/scatter_temp_cnt_holiday.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究temp和cnt的关系，再按照hum对点分类(scatterplot带hue和size)\n",
    "sns.scatterplot(x=\"temp\", y=\"cnt\", data=train[[\"temp\", \"cnt\", \"hum\"]], size=\"hum\")\n",
    "plt.title(\"cnt and temp relationship(divided by hum)\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![temp和cnt的关系-scatterplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/scatter_temp_cnt_hum.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究temp和cnt的关系, 再按照hum对点分类(scatterplot带hue/size/sizes)\n",
    "sns.scatterplot(x=\"temp\", y=\"cnt\", data=train[[\"temp\", \"cnt\", \"hum\"]], size=\"hum\", sizes=(2, 100))\n",
    "plt.title(\"cnt and temp relationship(divided by hum)\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![temp和cnt的关系-scatterplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/scatter_temp_cnt_hum_sizes.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究temp和cnt的关系(lineplot)\n",
    "sns.lineplot(x=\"temp\", y=\"cnt\", data=train[[\"temp\", \"cnt\"]])\n",
    "plt.title(\"cnt and temp relationship\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()\n",
    "\n",
    "sns.lineplot(x=\"temp\", y=\"cnt\", data=train[[\"temp\", \"cnt\"]], ci=None)\n",
    "plt.title(\"cnt and temp relationship\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()\n",
    "\n",
    "sns.lineplot(x=\"temp\", y=\"cnt\", data=train[[\"temp\", \"cnt\"]], ci=\"sd\")\n",
    "plt.title(\"cnt and temp relationship\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究temp和cnt的关系, 用yr做分割，分别画出lineplot\n",
    "sns.lineplot(x=\"temp\", y=\"cnt\", data=train[[\"temp\", \"cnt\", \"yr\"]], hue=\"yr\")\n",
    "plt.title(\"cnt and temp relationship\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()\n",
    "\n",
    "sns.lineplot(x=\"temp\", y=\"cnt\", data=train[[\"temp\", \"cnt\", \"yr\"]], hue=\"yr\", style=\"yr\")\n",
    "plt.title(\"cnt and temp relationship\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![temp和cnt的关系-lineplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/line_temp_cnt_yr.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究mnth和cnt的关系\n",
    "sns.lineplot(x=\"mnth\", y=\"cnt\", data=train[[\"mnth\", \"cnt\"]])\n",
    "plt.title(\"cnt and mnth relationship\")\n",
    "plt.xlabel(\"mnth\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究temp和cnt的关系,并按照season分组，再按照yr分子图观察\n",
    "sns.relplot(x=\"temp\", y=\"cnt\", kind=\"scatter\", data=train[[\"temp\", \"cnt\", \"season\", \"yr\"]], hue=\"season\", col=\"yr\")\n",
    "plt.title(\"cnt and temp relationship\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![temp和cnt的关系-scatterplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/scatter_temp_cnt_season_yr.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究temp和cnt的关系，并按照season分颜色, 再按照yr分列, 按照holiday分行\n",
    "sns.relplot(x=\"temp\", y=\"cnt\", kind=\"scatter\", \n",
    "            data=train[[\"temp\", \"cnt\", \"season\", \"yr\", \"holiday\"]], hue=\"season\", col=\"yr\", row=\"holiday\")\n",
    "plt.title(\"cnt and temp relationship\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![temp和cnt的关系-scatterplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/scatter_temp_cnt_season_holiday_yr.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究temp和cnt的关系，并按照season分颜色，再按照mnth分子图，每行最多包含4列\n",
    "sns.relplot(x=\"temp\", y=\"cnt\", kind=\"scatter\",\n",
    "            data=train[[\"temp\", \"cnt\", \"season\", \"mnth\"]], hue=\"season\", col=\"mnth\", col_wrap=4)\n",
    "plt.title(\"cnt and temp relationship\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![temp和cnt的关系-scatterplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/scatter_temp_cnt_season_mnth.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究离散型特征season和cnt的关系\n",
    "sns.catplot(x=\"season\", y=\"cnt\", data=train[[\"season\", \"cnt\"]])\n",
    "plt.title(\"cnt and season relationship\")\n",
    "plt.xlabel(\"season\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()\n",
    "\n",
    "sns.catplot(x=\"season\", y=\"cnt\", jitter=False, data=train[[\"season\", \"cnt\"]])\n",
    "plt.title(\"cnt and season relationship\")\n",
    "plt.xlabel(\"season\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()\n",
    "\n",
    "sns.stripplot(x=\"season\", y=\"cnt\", data=train[[\"season\", \"cnt\"]])\n",
    "plt.title(\"cnt and season relationship\")\n",
    "plt.xlabel(\"season\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()\n",
    "\n",
    "sns.swarmplot(x=\"season\", y=\"cnt\", data=train[[\"season\", \"cnt\"]])\n",
    "plt.title(\"cnt and season relationship\")\n",
    "plt.xlabel(\"season\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![season和cnt的关系-catplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/scatter_season_cnt_catplot.png)\n",
    "![season和cnt的关系-catplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/scatter_season_cnt_catplot_jitter_none.png)\n",
    "![season和cnt的关系-stripplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/scatter_season_cnt_stripplot.png)\n",
    "![season和cnt的关系-swarmplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/season_cnt_swarmplot.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究离散型特征season和响应cnt的关系, 并加入holiday特征作为着色依据\n",
    "sns.stripplot(x=\"season\", y=\"cnt\", hue=\"holiday\", data=train[[\"season\", \"cnt\", \"holiday\"]])\n",
    "plt.title(\"cnt and season relationship\")\n",
    "plt.xlabel(\"season\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()\n",
    "\n",
    "sns.swarmplot(x=\"season\", y=\"cnt\", hue=\"holiday\", data=train[[\"season\", \"cnt\", \"holiday\"]])\n",
    "plt.title(\"cnt and season relationship\")\n",
    "plt.xlabel(\"season\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![season和cnt的关系-stripplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/season_cnt_stripplot_hue_holiday.png)\n",
    "![season和cnt的关系-swarmplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/season_cnt_swarmplot_hue_holiday.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究离散型特征season和响应cnt的关系, 用boxplot来表现\n",
    "sns.boxplot(x=\"season\", y=\"cnt\", data=train[[\"season\", \"cnt\"]])\n",
    "plt.title(\"cnt and season relationship\")\n",
    "plt.xlabel(\"season\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![season和cnt的关系-boxplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/season_cnt_boxplot.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究离散型特征season和响应cnt的关系，用boxplot来表现，并引入特征holiday作为着色依据特征\n",
    "sns.boxplot(x=\"season\", y=\"cnt\", hue=\"holiday\", data=train[[\"season\", \"cnt\", \"holiday\"]])\n",
    "plt.title(\"cnt and season relationship\")\n",
    "plt.xlabel(\"season\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()\n",
    "\n",
    "sns.boxplot(x=\"season\", y=\"cnt\", hue=\"holiday\", dodge=False, data=train[[\"season\", \"cnt\", \"holiday\"]])\n",
    "plt.title(\"cnt and season relationship\")\n",
    "plt.xlabel(\"season\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![season和cnt的关系-boxplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/season_cnt_boxplot_hue_holiday.png)\n",
    "![season和cnt的关系-boxplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/season_cnt_boxplot_hue_holiday_dodge_none.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究离散型特征season和响应cnt的关系，用boxenplot来表现\n",
    "sns.boxenplot(x=\"season\", y=\"cnt\", data=train[[\"season\", \"cnt\"]])\n",
    "plt.title(\"cnt and season relationship\")\n",
    "plt.xlabel(\"season\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![season和cnt的关系-boxenplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/season_cnt_boxenplot.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究离散型特征season和响应cnt的关系，用violinplot来表现\n",
    "sns.violinplot(x=\"season\", y=\"cnt\", data=train[[\"season\", \"cnt\"]])\n",
    "plt.title(\"cnt and season relationship\")\n",
    "plt.xlabel(\"season\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![season和cnt的关系-violinplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/season_cnt_violinplot.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究离散型特征season和响应cnt的关系，用violinplot来表现，并在其内部画出散点图\n",
    "g = sns.catplot(x=\"season\", y=\"cnt\", kind=\"violin\", inner=None, data=train[[\"season\", \"cnt\"]])\n",
    "sns.swarmplot(x=\"season\", y=\"cnt\", data=train[[\"season\", \"cnt\"]], ax=g.ax)\n",
    "plt.title(\"cnt and season relationship\")\n",
    "plt.xlabel(\"season\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![season和cnt的关系-violinplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/season_cnt_violinplot_swarmplot.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究离散型特征season和响应cnt的关系，用barplot来表现\n",
    "sns.barplot(x=\"season\", y=\"cnt\", hue=\"holiday\", data=train[[\"season\", \"cnt\", \"holiday\"]])\n",
    "plt.title(\"cnt and season relationship\")\n",
    "plt.xlabel(\"season\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![season和cnt的关系-barplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/season_cnt_barplot.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究离散型特征season的取值分布情况\n",
    "sns.countplot(x=\"season\", data=train[[\"season\"]])\n",
    "plt.title(\"season value distribution\")\n",
    "plt.xlabel(\"season\")\n",
    "plt.ylabel(\"count\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![season的分布情况countplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/season_countplot.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究离散型特征season和响应cnt的关系，用pointplot来表现\n",
    "sns.pointplot(x=\"season\", y=\"cnt\", hue=\"holiday\", data=train[[\"season\", \"cnt\", \"holiday\"]])\n",
    "plt.title(\"cnt and season relationship\")\n",
    "plt.xlabel(\"season\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![season和cnt的关系pointplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/season_cnt_pointplot.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究连续型特征temp的分布情况\n",
    "plt.rcParams[\"font.sans-serif\"] = [\"SimHei\"]\n",
    "sns.distplot(train[[\"temp\"]])\n",
    "plt.title(\"温度(temp)的分布情况--distplot\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"prob ratio\")\n",
    "plt.show()\n",
    "\n",
    "sns.distplot(train[[\"temp\"]], kde=False, rug=True)\n",
    "plt.title(\"温度(temp)的分布情况--distplot\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"prob ratio\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![temp的分布情况distplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/temp_distplot.png)\n",
    "![temp的分布情况distplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/temp_distplot_kde_none.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\JetBrains\\PyCharm 2018.3.5\\helpers\\pycharm_matplotlib_backend\\backend_interagg.py:62: UserWarning: Tight layout not applied. tight_layout cannot make axes width small enough to accommodate all axes decorations\n  self.figure.tight_layout()\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\python3\\lib\\site-packages\\matplotlib\\backends\\backend_agg.py:211: RuntimeWarning: Glyph 8722 missing from current font.\n  font.set_text(s, 0.0, flags=flags)\nD:\\python3\\lib\\site-packages\\matplotlib\\backends\\backend_agg.py:180: RuntimeWarning: Glyph 8722 missing from current font.\n  font.set_text(s, 0, flags=flags)\n"
     ]
    }
   ],
   "source": [
    "# 研究连续型特征temp和离散型特征season的分布情况\n",
    "sns.jointplot(x=\"temp\", y=\"season\", data=train[[\"temp\", \"season\"]])\n",
    "plt.title(\"温度(temp)和季节(season)的联合分布情况--jointplot\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"season\")\n",
    "plt.show()\n",
    "\n",
    "sns.jointplot(x=\"temp\", y=\"hum\", data=train[[\"temp\", \"hum\"]])\n",
    "plt.title(\"温度(temp)和湿度(hum)的联合分布情况--jointplot\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"hum\")\n",
    "plt.show()\n",
    "\n",
    "sns.jointplot(x=\"temp\", y=\"hum\", kind=\"kde\", data=train[[\"temp\", \"hum\"]])\n",
    "plt.title(\"温度(temp)和湿度(hum)的联合分布的核密度估计--jointplot\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"hum\")\n",
    "plt.show()\n",
    "\n",
    "g = sns.jointplot(x=\"temp\", y=\"hum\", kind=\"kde\", data=train[[\"temp\", \"hum\"]])\n",
    "g.plot_joint(plt.scatter, c=\"w\", s=30, linewidth=1, marker=\"+\")\n",
    "g.ax_joint.collections[0].set_alpha(0)\n",
    "g.set_axis_labels(\"$X$\", \"$Y$\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![temp和season的联合分布情况jointplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/temp_season_jointplot.png)\n",
    "![temp和hum的联合分布情况jointplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/temp_hum_jointplot.png)\n",
    "![temp和hum的联合分布的核密度估计jointplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/temp_hum_jointplot_kde.png)\n",
    "![temp和hum的联合分布的核密度估计jointplot和scatter](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/temp_hum_jointplot_kde_scatter.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 画出特征中两两之间的联合分布\n",
    "sns.pairplot(data=train)\n",
    "plt.title(\"特征之间两两构成的联合分布\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![所有特征中两两之间的联合分布情况pairplot](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/pairplot.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究temp和atemp之间的关系，用简单的regplot回归曲线来近似\n",
    "sns.regplot(x=\"temp\", y=\"atemp\", data=train[[\"temp\", \"atemp\"]])\n",
    "plt.title(\"atemp和temp之间的关系\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"atemp\")\n",
    "plt.show()\n",
    "\n",
    "# 研究temp和cnt之间的关系，用简单的regplot回归曲线来近似\n",
    "sns.regplot(x=\"temp\", y=\"cnt\", data=train[[\"temp\", \"cnt\"]])\n",
    "plt.title(\"cnt和temp之间的关系\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![temp和atemp的线性近似](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/temp_atemp_regplot.png)\n",
    "![temp和cnt的线性近似](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/temp_cnt_regplot.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究temp和atemp之间的关系，用简单的lmplot回归曲线来近似\n",
    "sns.lmplot(x=\"temp\", y=\"atemp\", data=train[[\"temp\", \"atemp\"]])\n",
    "plt.title(\"atemp和temp之间的关系\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"atemp\")\n",
    "plt.show()\n",
    "\n",
    "sns.lmplot(x=\"temp\", y=\"cnt\", data=train[[\"temp\", \"cnt\"]])\n",
    "plt.title(\"cnt和temp之间的关系\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()\n",
    "\n",
    "sns.lmplot(x=\"mnth\", y=\"cnt\", data=train[[\"mnth\", \"cnt\"]], x_estimator=np.mean)\n",
    "plt.title(\"cnt和mnth的关系\")\n",
    "plt.xlabel(\"mnth\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![temp和atemp的线性近似](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/temp_atemp_lmplot.png)\n",
    "![temp和cnt的线性近似](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/temp_cnt_lmplot.png)\n",
    "![mnth和cnt的线性近似](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/mnth_cnt_lmplot.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究temp和atemp之间的关系，用简单的lmplot来近似，再用residplot来画出误差\n",
    "sns.lmplot(x=\"temp\", y=\"atemp\", data=train[[\"temp\", \"atemp\"]])\n",
    "plt.title(\"atemp和temp之间的关系\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"atemp\")\n",
    "plt.show()\n",
    "\n",
    "sns.lmplot(x=\"temp\", y=\"cnt\", hue=\"holiday\", markers=[\"o\", \"x\"], data=train[[\"temp\", \"cnt\", \"holiday\"]])\n",
    "plt.title(\"cnt和temp之间的关系\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"cnt\")\n",
    "plt.show()\n",
    "\n",
    "sns.residplot(x=\"temp\", y=\"atemp\", data=train[[\"temp\", \"atemp\"]])\n",
    "plt.title(\"atemp和temp之间的关系\")\n",
    "plt.xlabel(\"temp\")\n",
    "plt.ylabel(\"atemp\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![temp和atemp的线性近似](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/temp_atemp_lmplot.png)\n",
    "![temp和atemp的线性近似的误差](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/temp_atemp_residplot.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 研究所有特征之间的相关性图示heatmap\n",
    "plt.subplots(figsize=(11, 11))\n",
    "corr = train.drop(labels=[\"instant\", \"casual\", \"registered\", \"cnt\"], axis=1, inplace=False).corr()\n",
    "mask = np.array(corr)\n",
    "mask[np.tril_indices_from(mask)] = False\n",
    "sns.heatmap(corr, mask=mask, vmin=0, vmax=.8, square=True, annot=True, linewidths=.8, fmt=\".2f\")\n",
    "plt.show()\n",
    "\n",
    "# 研究连续型特征之间的相关性图示heatmap\n",
    "plt.subplots(figsize=(11, 11))\n",
    "corr = train[[\"temp\", \"atemp\", \"hum\", \"windspeed\", \"cnt\"]].corr()\n",
    "mask = np.array(corr)\n",
    "mask[np.tril_indices_from(mask)] = False\n",
    "sns.heatmap(corr, mask=mask, vmin=0, vmax=.8, square=True, annot=True, linewidths=.8, fmt=\".2f\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![train中特征之间的相关性图示](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/train_corr_heatmap.png)\n",
    "根据图示可以知道，\n",
    "temp和atemp相关性强;\n",
    "hum和season,mnth,weathersit相关性强;\n",
    "season和mnth相关性强;\n",
    "season和temp,atemp相关性强;\n",
    "mnth和temp,atemp相关性强;\n",
    "这些相关性，都是符合直觉的。\n",
    "\n",
    "![train中连续型特征之间的相关性图示](https://gitee.com/coolhenry/week_4__machine_learning/raw/master/out/bike_sharing/train_statis_corr_heatmap.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
